﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Model;
using Jzy.DAL.Mail;
using Jzy.Model.Mail;
using System.Data;

using Frame.Lib.Core.Base;
using Frame.Lib.Common;
using Frame.Lib.Utilities;

namespace Jzy.BLL.Mail
{
    public class MailFolderBO:CommonOperate<MailFolderInfo>
    {
        private MailFolderDAL _mailFolder;
        protected override BaseDAL DAL
        {
            get
            {
                return _mailFolder;
            }
        }
        private int _userID;
        MailFolderType _folderType;
        public MailFolderBO(int userID,MailFolderType typeID):base("MailFolder")
        {
            _userID = userID;
            _folderType = typeID;
            _mailFolder = new MailFolderDAL(userID, typeID);
        }

        /// <summary>
        /// 获得用户的自定义邮箱
        /// </summary>
        /// <returns></returns>
        public override List<MailFolderInfo> GetList()
        {
            return _mailFolder.GetList();
        }

        /// <summary>
        /// 获得用户自定义和系统邮箱并转换成字典
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> GetAllBoxDictionay()
        {
            IEnumerable<MailFolderInfo> boxs = _mailFolder.GetList();
            Dictionary<int, string> allboxs = new Dictionary<int, string>();
            allboxs.Add((int)MailSysFolder.已删除, "已删除");
            allboxs.Add((int)MailSysFolder.草稿箱, "草稿箱");
            allboxs.Add((int)MailSysFolder.收件箱, "收件箱");
            allboxs.Add((int)MailSysFolder.已发送, "发件箱");
            foreach (MailFolderInfo info in boxs)
            {
                allboxs.Add(info.ID, info.Name);
            }
            return allboxs;
        }

        /// <summary>
        /// 获得用户自定义和系统邮箱
        /// </summary>
        /// <returns></returns>
        public List<MailFolderInfo> GetAllBoxList()
        {
            List<MailFolderInfo> boxes = _mailFolder.GetList();
            int[] values = (int[])Enum.GetValues(typeof(MailSysFolder));
            foreach (int i in values)
            {
                MailFolderInfo info = new MailFolderInfo();
                info.ID = i;
                info.Name = Enum.GetName(typeof(MailSysFolder), i);
                info.UserID = AppSession.UserID;
                boxes.Insert(0, info);
            }
            return boxes;     
        }
        public DataTable GetCountList()
        {
            
            DataTable dtFolder = _mailFolder.GetDataTable();
            dtFolder.Columns.Add("IsCustFolder", typeof(bool));

            dtFolder.Columns.Add("UnReadCount", typeof(int));
            dtFolder.Columns.Add("AllCount", typeof(int));
            dtFolder.Columns.Add("SpaceCount", typeof(string));

            int[] values = (int[])Enum.GetValues(typeof(MailSysFolder));
            foreach (int i in values)
            {
                DataRow dr = dtFolder.NewRow();
                dr["ID"] = i;
                dr["Name"] = Enum.GetName(typeof(MailSysFolder), i);
                dr["UserID"] = AppSession.UserID;
                dr["IsCustFolder"] = false;

                dtFolder.Rows.InsertAt(dr, 0);                
            }

            DataTable dt = _mailFolder.GetCountList();
            foreach (DataRow dr in dtFolder.Rows)
            {
                dr["UnReadCount"] = 0;
                DataRow[] rows = dt.Select("IsAll=0 and FolderID = " + dr["ID"]);
                if (rows.Length == 1)
                {
                    dr["UnReadCount"] = rows[0]["Count"];
                }

                dr["AllCount"] = 0;
                rows = dt.Select("IsAll=1 and FolderID = " + dr["ID"]);
                if (rows.Length == 1)
                {
                    dr["AllCount"] = rows[0]["Count"];
                }

                if (dr["IsCustFolder"] is DBNull)
                {
                    dr["IsCustFolder"] = true ;
                }
            }

            return dtFolder;
        }

        public void AddCustMailFolder(int moduleID,bool isUpdate)
        {
            AddCustMailFolder(moduleID, isUpdate, GlobalVar.WebCurrPath);            
        }

        public void AddCustMailFolder(int moduleID, bool isUpdate, string destPath)
        {
            IEnumerable<MailFolderInfo> mailBoxs = GetList();
            StringBuilder sbHTML = new StringBuilder();
            if (mailBoxs.Count() > 0)
            {
                sbHTML.Append("<div class='seperator'>&nbsp;</div>");
            }
            foreach (MailFolderInfo info in mailBoxs)
            {
                sbHTML.Append("<div class='module3'><a href='javascript:;' hidefocus='hidefocus' onmouseover='mouseOver(this)' onmouseout='mouseOut(this)' onclick='navModule3(this,\\\"" + destPath + "List.aspx?FolderID=" + info.ID + "\\\");'><b style='background-image:url(" + GlobalVar.WebAppPath + "common/images/module/ico_031.gif)'></b>" + info.Name + "</a></div>");
            }

            JSUtil.AddModule3(moduleID, sbHTML.ToString(), isUpdate);
        }


        public void Clear(object folderID)
        {
            if (_folderType == MailFolderType.内部邮件)
            {
                MailBO mail = new MailBO(_userID);
                mail.Clear(folderID);
            }
            else
            {
                WebMailBO mail = new WebMailBO(_userID);
                mail.Clear(folderID);
            }
        }
    }
}
